﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace CNKI.Graduate.Framework.Platform
{

    public static class EnumExtensions
    {
        /// <summary>
        /// 获取枚举字段的描述信息
        /// </summary>
        /// <param name="fieldEnum"></param>
        /// <returns></returns>
        public static string GetEnumFieldText(this Enum fieldEnum)
        {
            string objName = fieldEnum.ToString();
            Type t = fieldEnum.GetType();
            FieldInfo fi = t.GetField(objName);
            DescriptionAttribute arrDesc = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
            if (arrDesc != null)
                return arrDesc.Description;
            return fieldEnum.ToString();
        }
        /// <summary>
        /// 获取枚举类型的描述信息
        /// </summary>
        /// <param name="Enum"></param>
        /// <returns></returns>
        public static string GetEnumText(this Enum Enum)
        {
            Type type = Enum.GetType();
            if (!type.IsDefined(typeof(DescriptionAttribute), false))
                return type.ToString();

            DescriptionAttribute attribute = type.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
            if (attribute != null)
                return attribute.Description;
            return type.ToString();
        }
    }

}
